/*
 * File:   hardware_settings.c
 * Author:  Dominik H�ussler
 *          Ruben Schwarz
 *
 * Created on 9. M�rz 2011, 14:30
 */

#include "hardware_settings.h"

void initClock(void) {
    /* Set CPU IPL to 0, enable level 1-7 interrupts */
    /* No restoring of previous CPU IPL state performed here */
    SRbits.IPL = 0;
    /*
        //Konfiguriere Clock für 37Mhz
        PLLFBD = 38; // M = 40
        CLKDIVbits.PLLPOST = 0; // N1 = 2
        CLKDIVbits.PLLPRE = 0; // N2 = 2
        OSCTUN = 0;
        RCONbits.SWDTEN = 0;

        // Clock switch to incorporate PLL
        __builtin_write_OSCCONH(0x01); // Initiate Clock Switch to
        // FRC with PLL (NOSC=0b001)
        __builtin_write_OSCCONL(0x01); // Start clock switching
        while (OSCCONbits.COSC != 0b001); // Wait for Clock switch to occur

        while (OSCCONbits.LOCK != 1) {
        };
     */

    // Configure PLL prescaler, PLL postscaler, PLL divisor
    PLLFBD = 41;// M = 43
    CLKDIVbits.PLLPOST = 0; // N2 = 2
    CLKDIVbits.PLLPRE = 0;  // N1 = 2
    // Initiate Clock Switch to Internal FRC with PLL (NOSC = 0b001)
    __builtin_write_OSCCONH(0x01);
    __builtin_write_OSCCONL(0x01);
    // Wait for Clock switch to occur
    while (OSCCONbits.COSC != 0b001);
    // Wait for PLL to lock
    while (OSCCONbits.LOCK != 1) {
    };

}

void initRemappablePins(void)
{
	//dsPIC33FJ12MC202 28Pin Layout specific

  //uart1
  TRISBbits.TRISB5 = 1; //RB5 set to input
  TRISBbits.TRISB6 = 1; //RB6 set to output
  //remapable registers
  RPOR3bits.RP6R = 3; //RP6 tied to UART1 Transmit
  RPINR18bits.U1RXR = 0b0101; //RP5 -> 0b0011
}
